package com.rtx.edi.platform.quartz.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.annotation.RtxJobAnnotation;
import com.rtx.api.quartz.job.BaseJob;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.quartz.bean.JobDto;
import com.rtx.edi.platform.quartz.bean.JobEntity;
import com.rtx.edi.platform.quartz.service.JobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 定时任务
 */
@Api(tags = {"调度任务管理"})
@RestController
@RequestMapping(value = "/v1/job")
@CrossOrigin
public class JobController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private JobService jobService;
	
	@Autowired
	ApplicationContext context;
	
	
	@ApiOperation(value = "获取标准定时任务", notes = "获取标准定时任务")
	@PostMapping("selectSTDJob")
	public EdiResponse querySTDJob(@RequestBody String request) {
		String[] names =  context.getBeanNamesForAnnotation(RtxJobAnnotation.class);
		
		
		List<Map<String,String>>jobs =new ArrayList<>();
		
		Map<String,String> nulljob =new HashMap<String, String>();
		nulljob.put("label", "");
		nulljob.put("value", "");
		jobs.add(nulljob);
		
		for(String name:names) {
			BaseJob job = (BaseJob) context.getBean(name);
			RtxJobAnnotation jobAnnotation = job.getClass().getAnnotation(RtxJobAnnotation.class);
			String jobName = jobAnnotation.description();
			Map<String,String> stdjob =new HashMap<String, String>();
			stdjob.put("label", jobName);
			stdjob.put("value", job.getClass().getName());
			jobs.add(stdjob);
		}

		
		return EdiResponse.success().data("item", jobs);
	}
	
	/**
	 * 新增定时任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "新增调度任务", notes = "新增调度任务")
	@PostMapping("addJob")
	public EdiResponse add(@RequestBody JobDto jobDto){
		try {
			JobEntity jobEntity = new JobEntity();
			BeanUtils.copyProperties(jobDto,jobEntity);
			jobService.add(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，添加失败",jobDto.getJobClassName(),jobDto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}

	/**
	 * 删除任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "删除调度任务", notes = "删除调度任务")
	@PostMapping("deleteJob")
	public EdiResponse delete(@RequestBody JobDto jobDto){
		try {
			JobEntity jobEntity = new JobEntity();
			BeanUtils.copyProperties(jobDto,jobEntity);
			jobService.delete(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，删除失败",jobDto.getJobClassName(),jobDto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}

	/**
	 * 修改任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "修改调度任务", notes = "修改调度任务")
	@PostMapping("editJob")
	public EdiResponse update(@RequestBody JobDto jobDto){
		try {
			JobEntity jobEntity = new JobEntity();
			BeanUtils.copyProperties(jobDto,jobEntity);
			jobService.update(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，修改失败",jobDto.getJobClassName(),jobDto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}

	/**
	 * 获取定时任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "调度任务列表", notes = "分页获取调度任务列表")
	@PostMapping("selectJobList")
	public EdiResponse pageList(@RequestBody(required = false) JobDto jobDto){
		JobEntity jobEntity = new JobEntity();
		BeanUtils.copyProperties(jobDto,jobEntity);
		Page<JobEntity> pageParam = new Page<>(jobDto.getPageNum(), jobDto.getPageSize());
		jobService.pageList(pageParam,jobEntity);
		return EdiResponse.success().data("page",pageParam);
	}

	/**
	 * 执行任务
	 * @param jobEntity
	 * @return
	 */
	@ApiOperation(value = "执行调度任务", notes = "执行调度任务")
	@PostMapping("executeJob")
	public EdiResponse execute(@RequestBody JobEntity jobEntity){
		try {
			jobService.execute(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，执行失败",jobEntity.getJobClassName(),jobEntity.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}

	/**
	 * 暂停任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "暂停调度任务", notes = "暂停调度任务")
	@PostMapping("pauseJob")
	public EdiResponse pause(@RequestBody JobDto jobDto){
		try {
			JobEntity jobEntity = new JobEntity();
			BeanUtils.copyProperties(jobDto,jobEntity);
			jobService.pause(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，暂停失败",jobDto.getJobClassName(),jobDto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}

	/**
	 * 恢复任务
	 * @param jobDto
	 * @return
	 */
	@ApiOperation(value = "恢复调度任务", notes = "恢复调度任务")
	@PostMapping("resumeJob")
	public EdiResponse resume(@RequestBody JobDto jobDto){
		try {
			JobEntity jobEntity = new JobEntity();
			BeanUtils.copyProperties(jobDto,jobEntity);
			jobService.resume(jobEntity);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务类：{}，定时任务描述：{}，恢复失败",jobDto.getJobClassName(),jobDto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
}
